package com.zyy.java8.samples.lambda;

/**
 * @author zhangyiying
 * @ClassName: Lambda2
 * @Description: 函数式接口：只定义了一个抽象方法的接口（Object类的public方法除外） 函数式接口的提出是为了给Lambda表达式的使用提供更好的支持。
 * @date 2021/12/23 14:21
 */
public class Lambda2 {

    @FunctionalInterface
    public static interface Converter<F, T> {
        T convert(F from);
    }

    static class Something {
        String startWith(String s) {
            return String.valueOf(s.charAt(0));
        }
    }

    interface PersonFactory<P extends Person> {
        P create(String firstName, String lastName);
    }


    public static void main(String[] args) {
        Converter<String, Integer> integerConverter1 = from -> Integer.valueOf(from);
        Integer converted1 = integerConverter1.convert("123");
        //result:123
        System.out.println(converted1);

        // 方法引用 method reference

        Converter<String, Integer> integerConverter2 = Integer::valueOf;
        Integer converted2 = integerConverter2.convert("123");
        //result:123
        System.out.println(converted2);

        Something something = new Something();

        Converter<String, String> stringConverter = something::startWith;
        String converted3 = stringConverter.convert("Java");
        //result J
        System.out.println(converted3);

        // 构造函数引用 constructor

        PersonFactory<Person> personFactory = Person::new;
        Person person = personFactory.create("Peter", "Parker");
    }
}
